package me.travelbug.gulimall.product.controller;

import lombok.extern.slf4j.Slf4j;
import me.travelbug.gulimall.common.to.SpuBoundsTo;
import me.travelbug.gulimall.common.utils.R;
import me.travelbug.gulimall.product.entity.AttrAttrgroupRelationEntity;
import me.travelbug.gulimall.product.feign.CouponFeignService;
import me.travelbug.gulimall.product.feign.WareFeignService;
import me.travelbug.gulimall.product.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 属性&属性分组关联
 *
 * @author travelbug
 * @email java_shuai@163.com
 * @date 2022-04-11 22:24:48
 */
@Slf4j
@RestController
@RequestMapping("product/attrattrgrouprelation")
public class AttrAttrgroupRelationController {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CouponFeignService couponFeignService;


    @Autowired
    private WareFeignService wareFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrattrgrouprelation:list")
    public R list(@RequestParam Map<String, Object> params) {
//        PageUtils page = attrAttrgroupRelationService.queryPage(params);
//        return R.ok().put("page", page);

        // 以下为测试的代码

//        String forObject = restTemplate.getForObject("http://gulimall-ware/ware/purchase/list", String.class);
//        System.out.println("nacos===============" + forObject);

//        SpuBoundsTo spuBoundsTo = SpuBoundsTo.builder().spuId(1L).growBounds(new BigDecimal("1")).buyBounds(new BigDecimal("1")).work(1).build();
//        couponFeignService.save(spuBoundsTo);
//        return R.ok().put("page", spuBoundsTo);

        R list = wareFeignService.list(new HashMap<>());
        log.info("执行了远程调用ware............");
        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:attrattrgrouprelation:info")
    public R info(@PathVariable("id") Long id) {
        AttrAttrgroupRelationEntity attrAttrgroupRelation = attrAttrgroupRelationService.getById(id);

        return R.ok().put("attrAttrgroupRelation", attrAttrgroupRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrattrgrouprelation:save")
    public R save(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        attrAttrgroupRelationService.save(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrattrgrouprelation:update")
    public R update(@RequestBody AttrAttrgroupRelationEntity attrAttrgroupRelation) {
        attrAttrgroupRelationService.updateById(attrAttrgroupRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrattrgrouprelation:delete")
    public R delete(@RequestBody Long[] ids) {
        attrAttrgroupRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
